/*
 * File:         AuthenticationServlet.java
 * Author:       Robert Bittle <guywithnose@gmail.com>
 */
package servlets;

import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;
import data.JavaCurl;

/**
 * The Class AuthenticationServlet.
 *
 * @author Dude
 */
public class AuthenticationServlet extends FoxtrotBravoServlet
{

  private static final Logger log = Logger.getLogger(AuthenticationServlet.class
      .getName());
  
  /**
   * Authenticate.
   *
   * @param userId the user id
   * @param accessToken the access token
   * @return true, if successful
   */
  protected static boolean authenticate(long userId, String accessToken)
  {
    String response = JavaCurl
        .getUrl("https://graph.facebook.com/me?fields=id&access_token=" + accessToken);
    try
    {
      JSONObject userInfo = new JSONObject(response);
      return userInfo.getLong("id") == userId;
    } catch (JSONException e)
    {
      log.info("UserId: " + userId);
      log.info("Authentication failed: " + e.toString());
    }
    return false;
  }
  
}
